package com.cardone.platform.common.service;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.cardone.common.cache.Caches;
import com.cardone.common.cache.util.CacheUtils;
import com.cardone.common.dto.PaginationDto;
import com.cardone.context.ContextHolder;
import com.cardone.context.DictionaryException;
import com.cardone.context.function.Execution0Function;
import com.cardone.platform.common.dao.EntityLogDao;
import com.cardone.platform.common.dto.EntityLogDto;
import com.cardone.platform.configuration.service.DictionaryService;

/**
 * 实体日志服务
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
@Transactional
public class EntityLogDefaultService implements EntityLogService {
	@Override
	@Transactional(readOnly = false)
	public int deleteByIds(final EntityLogDto deleteEntityLog) {
		return ContextHolder.getBean(EntityLogDao.class).deleteByIds(deleteEntityLog);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final EntityLogDto findEntityLog) {
		return ContextHolder.getBean(EntityLogDao.class).findByCodeOrId(mappedClass, findEntityLog);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final EntityLogDto findEntityLog) {
		return ContextHolder.getBean(EntityLogDao.class).findById(mappedClass, findEntityLog);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final EntityLogDto findListEntityLog) {
		return ContextHolder.getBean(EntityLogDao.class).findListByLikeCode(mappedClass, findListEntityLog);
	}

	@Override
	@Transactional(readOnly = false)
	public int insertByCode(final EntityLogDto insertEntityLog) {
		final String typeId = ContextHolder.getBean(DictionaryService.class).readIdByTypeCodeAndCode(insertEntityLog.getTypeTypeCode(), insertEntityLog.getTypeCode());

		if (StringUtils.isBlank(typeId)) {
			throw new DictionaryException("不支持的日志类别");
		}

		insertEntityLog.setTypeId(typeId);

		return ContextHolder.getBean(EntityLogDao.class).insertByCode(insertEntityLog);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final EntityLogDto paginationEntityLog) {
		return ContextHolder.getBean(EntityLogDao.class).paginationByLikeCode(mappedClass, paginationEntityLog);
	}

	@Override
	public Integer readByCodeNqIdForCount(final EntityLogDto readEntityLog) {
		return ContextHolder.getBean(EntityLogDao.class).readByCodeNqIdForCount(readEntityLog);
	}

	@Override
	public String readEntityIdByIdForValid(final String id) {
		return CacheUtils.getCacheValue(Caches.loginKey.name(), id, new Execution0Function<String>() {
			@Override
			public String execution() {
				return ContextHolder.getBean(EntityLogDao.class).readEntityIdByIdForValid(id);
			}
		});
	}

	@Override
	public int saveByEntityIdAndTypeId(final EntityLogDto saveEntityLog) {
		final String typeId = ContextHolder.getBean(DictionaryService.class).readIdByTypeCodeAndCode(saveEntityLog.getTypeTypeCode(), saveEntityLog.getTypeCode());

		if (StringUtils.isBlank(typeId)) {
			throw new DictionaryException("不支持的日志类别");
		}

		saveEntityLog.setTypeId(typeId);

		final int saveCount = ContextHolder.getBean(EntityLogDao.class).saveByEntityIdAndTypeId(saveEntityLog);

		CacheUtils.getCache(Caches.loginKey.name()).clear();

		return saveCount;
	}

	@Override
	@Transactional(readOnly = false)
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final EntityLogDto saveEntityLog) {
		return ContextHolder.getBean(EntityLogDao.class).saveByIdOrCode(mappedClass, saveEntityLog);
	}

	@Override
	@Transactional(readOnly = false)
	public int updateByCode(final EntityLogDto updateEntityLog) {
		return ContextHolder.getBean(EntityLogDao.class).updateByCode(updateEntityLog);
	}
}